/*
 * @(#)IHEConfigProducer.java
 * Date 2013-04-11
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.ihecommon.inject;

import java.io.InputStream;
import javax.enterprise.inject.Alternative;

import javax.enterprise.inject.Produces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.yaml.snakeyaml.Yaml;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;



/**
 *
 * @author jim
 */
@Alternative
public class IHEConfigProducer {

    /** Field description */
    public static final Logger logger = LoggerFactory.getLogger(IHEConfigProducer.class);

    /**
     * Method description
     *
     *
     * @param is
     *
     * @return
     */
    public static IHEConfig readConfig(InputStream is) {

        Yaml yaml = new Yaml(new IHEConfigConstructor());
        IHEConfig result = yaml.loadAs(is, IHEConfig.class);

        result.setLoaded(true);

        return result;
    }

    /**
     * Method description
     *
     *
     *
     * @return
     */
    @Produces
    public IHEConfig produce() {

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream is = cl.getResourceAsStream("META-INF/IHEConfig.yml");

        if (is == null) {
            
            String msg = "Missing META-INF/IHEConfig.yml";
            logger.error(msg);
            throw new IllegalStateException(msg);
        }

        return readConfig(is);
    }
}
